import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { MessageService } from './../core/message.service';
import { ToptipsService } from 'ngx-weui';
import { ApiService } from 'src/app/core/api';

@Component({
	templateUrl: './withdraw.html',
	styleUrls: ['./withdraw.less'],
})
export class WithdrawComponent implements OnInit {
	data: any = {};
	active = 1;
	type;
	coinType;
	password = '';
	res: any = {};
	constructor(private message: MessageService, private route: Router, private activatedRoute: ActivatedRoute, private srv: ToptipsService, private apiService: ApiService) {
		this.message.sendMessage('my');
	}
	ngOnInit() {
		this.activatedRoute.params.subscribe((params: Params) => {
			this.type = params['type'];
		});
		this.apiService.post('获取余额和提现费率').then((res: any) => {
			this.res = res.data;
		});
	}
	sure(e) {}
	showThis(i) {
		if (this.active !== i) {
			this.active = i;
		}
	}
	back(){
		if(this.active === 1){
			this.route.navigateByUrl('/indexMy');
		}else{
			this.active = 1;
		}
	}
	next() {
		if (!this.data.toAddress) {
			this.srv.warn('请填写收款地址');
			return;
		}
		if (this.data.toAddress.length < 18) {
			this.srv.warn('转账地址不能小于18位');
			return;
		}
		if (!this.data.amount) {
			this.srv.warn('请填写转账金额');
			return;
		}
		if (!/^[+-]?(0|([1-9]\d*))(\.\d+)?$/g.test(this.data.amount) || this.data.amount == 0) {
			this.srv.warn('请填写正确的转账金额');
			return;
		}
		if (this.data.amount < this.res.withdrawalMinAmount) {
			this.srv.warn('转账金额不能小于' + this.res.withdrawalMinAmount);
			return;
		}
		console.log(this.data);
		if (this.coinType === 'Y') {
			if (Number(this.res.availableBalance - this.res.coin.withdrawalInnerFeeAmount).toFixed(2) < this.data.amount) {
				this.srv.warn('余额不足，转账金额不能大于' + Number(this.res.availableBalance - this.res.coin.withdrawalInnerFeeAmount).toFixed(2));
				return;
			}
		} else if (this.coinType === 'N') {
			if (Number(this.res.availableBalance - this.res.coin.withdrawalFeeAmount).toFixed(2) < this.data.amount) {
				this.srv.warn('余额不足，转账金额不能大于' + Number(this.res.availableBalance - this.res.coin.withdrawalFeeAmount).toFixed(2));
				return;
			}
		}
		this.active = 2;
	}
	sub() {
        console.log(this.password);
        if(!this.data.tradePw||this.data.tradePw.length<6){
            this.srv.warn('请填写6位数字支付密码');
            return;
        }
        this.apiService.post('用户请求提币',this.data).then(res=>{
            this.srv.success('提币成功');
            this.route.navigateByUrl('/indexMy');
        });
	}
	validAddress() {
		if (this.data.toAddress&&this.data.toAddress.length > 18) {
			this.apiService.post('判断是否为平台内或外', { address: this.data.toAddress }).then((res: any) => {
				this.coinType = res.data.internalTransfer;
			});
		}
	}
	getAll() {
		if (!this.data.toAddress) {
			this.srv.warn('请填写转账地址');
			return;
		}
		if (this.data.toAddress.length < 18) {
			this.srv.warn('转账地址不能小于18位');
			return;
		}
		if (this.coinType === 'Y') {
			this.data.amount = (this.res.availableBalance - this.res.coin.withdrawalInnerFeeAmount).toFixed(2);
		} else if (this.coinType === 'N') {
			this.data.amount = (this.res.availableBalance - this.res.coin.withdrawalFeeAmount).toFixed(2);
		}
	}
}
